//
//  ChooseView.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/10/19.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "ChooseView.h"
#import "RegisterShopModel.h"
@interface ChooseView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic,strong)NSMutableArray *dataArr;

@property(nonatomic,copy)ChooseFinshBlock Oblock;

@property(nonatomic,copy)NSString*ChooseValue;

@property(nonatomic,assign)NSInteger ChooseIndex;

@end

@implementation ChooseView

-(instancetype)initWithFrame:(CGRect)frame andDataArr:(NSArray*)DataArr andBlock:(ChooseFinshBlock)block
{
    if (self=[super initWithFrame:frame]) {
  
        self=[[[NSBundle mainBundle]loadNibNamed:@"ChooseView" owner:self options:nil]lastObject];
        
        self.frame=frame;
        
        self.dataArr=[NSMutableArray array];
        
        [self.dataArr addObjectsFromArray:DataArr];
        
        self.DataPickView.delegate=self;
        
        self.DataPickView.dataSource=self;
        
        if (DataArr.count!=0) {
            
            RSItems *items=self.dataArr[0];
            
            self.ChooseValue=items.value;
            
            self.ChooseIndex=0;
            
        }
 
        self.Oblock=block;
        
        [self.DataPickView reloadAllComponents];
  
    }
    
    return self;
}

#pragma mark-窗口选择代理方法
//获取UIPickerView指定列中包含的列表项的数量
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    
    return 1;
}


//获取UIPickerView包含的列数量
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    
    return self.dataArr.count;
    
}

//列表项的高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    
    return 45;
    
}

//列表的内容
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    RSItems *items=self.dataArr[row];
    
    return items.name;
}


//点击时选择项
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (self.dataArr.count!=0) {
        
        RSItems *items=self.dataArr[row];
        
        self.ChooseValue=items.value;
        
        self.ChooseIndex=row;
        
    }
   
}

-(void)PopWithView:(UIView*)SupView
{
 
    [SupView addSubview:self];
    
    POPBasicAnimation *BaseA=[POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    
    BaseA.fromValue=@(0.0);
    
    BaseA.toValue=@(0.3);
    
    BaseA.duration=0.3;
    
    [self.BlackBgView pop_addAnimation:BaseA forKey:@"fade"];
    
    POPBasicAnimation *BaseB=[POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
    
    BaseB.fromValue=[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0, self.height+self.ContentBgView.height/2.0)];
    
    BaseB.toValue=[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0, self.height-self.ContentBgView.height/2.0)];
    
    BaseB.duration=0.3;
    
    [self.ContentBgView pop_addAnimation:BaseB forKey:@"dy"];

}

-(void)DissmissSupview
{
    POPBasicAnimation *BaseA=[POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    
    BaseA.fromValue=@(0.3);
    
    BaseA.toValue=@(0.0);
    
    BaseA.duration=0.3;
    
    [self.BlackBgView pop_addAnimation:BaseA forKey:@"fade"];
 
    POPBasicAnimation *BaseB=[POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
    
    BaseB.fromValue=[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0, self.height-self.ContentBgView.height/2.0)];
    
    BaseB.toValue=[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0, self.height+self.ContentBgView.height/2.0)];
    
    BaseB.duration=0.3;
    
    [self.ContentBgView.layer pop_addAnimation:BaseB forKey:@"dy"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self removeFromSuperview];
        
    });
    
    
}


- (IBAction)SureAction:(UIButton *)sender {
    
    self.Oblock(self.ChooseValue,self.ChooseIndex);
    
    [self DissmissSupview];
    
}
- (IBAction)CancleAction:(UIButton *)sender {
    
    [self DissmissSupview];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    
    if (![touch.view isEqual:self.ContentBgView]) {
        
        [self DissmissSupview];
    }
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
